<template>
  <div>
    <el-dialog
      :title="title"
      v-model="visable"
      width="45%"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 550px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 90%;padding-left: 5%;text-align: left;"
        >
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="练习账号:" prop="studentID">
              <el-select
                filterable
                v-model="dialogFormData.studentID"
                placeholder="请选择练习账号" 
                clearable
              >
                <el-option
                  v-for="item in studentList"
                  :key="item.id"
                  :label="item.code+'-'+item.wechatNickname+'-'+item.phone"
                  :value="item.id"
                >
                    <div>
                    <div style="display: inline-block;width: 200px;">
                      <span title="学员编号">{{ item.code }}</span>
                      <span title="微信昵称" style="float:right;color:#c7c7c7;margin-right: 5px;">{{ item.wechatNickname }}</span>
                    </div>
                    <span title="手机号码" style="display: inline-block;color: var(--el-color-primary);width: 90px;">{{ item.phone }}</span>
                    </div>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="充值日期:" prop="tradeDate">
              <el-date-picker
                v-model="dialogFormData.tradeDate"
                type="date"
                placeholder="请选择充值日期"
                size="small"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="累计盈亏:" prop="totalProfitLoss">
              <el-input-number v-model="dialogFormData.totalProfitLoss" placeholder="请输入应累计盈亏" @change="shouldRechargeChange" :controls="false"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="盈亏修正:" prop="profitLossAmend">
              <el-input-number v-model="dialogFormData.profitLossAmend" placeholder="请输入应盈亏修正" @change="shouldRechargeChange" :controls="false"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="充值比例:" prop="rate">
              <el-input-number v-model="dialogFormData.rate" max="1" placeholder="请输入未充值比例" :step="0.1" @change="shouldRechargeChange" clearable>
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应充值:" prop="shouldRecharge">
              <el-input-number v-model="dialogFormData.shouldRecharge" placeholder="请输入应充值金额" @change="unRechargeChange" :controls="false" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应充值修正:" prop="shouldRechargeAmend">
              <el-input-number  v-model="dialogFormData.shouldRechargeAmend" placeholder="请输入应充值修正" @change="unRechargeChange" :controls="false" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="今充值:" prop="thisRecharged">
              <el-input-number v-model="dialogFormData.thisRecharged" placeholder="请输入今充值" @change="unRechargeChange" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="待充值:" prop="unRecharge">
              <el-input-number v-model="dialogFormData.unRecharge" placeholder="请输入待充值金额"  clearable/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 3%;">
          <el-button  size="small" @click="visable = false"> 取消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { updateItem, addItem, getItemByID } from '@/api/oss/rechargeManage.js'
import { getAll } from '@/api/oss/student.js'
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        studentID:"",
        tradeDate:"",
        totalProfitLoss:undefined,
        profitLossAmend:undefined,
        shouldRecharge:undefined,
        shouldRechargeAmend:undefined,
        thisRecharged:undefined,
        unRecharge:undefined,
        rate:undefined,
        remark:undefined,
      }
export default {
  data() {
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        studentID: [
          { required: true, message: '请选择学员信息', trigger: ['blur', 'change'] },
        ],
        tradeDate: [
          { required: true, message: '请选择交易日期', trigger: ['blur', 'change'] },
        ]
      },
      // 学员列表
      studentList:[],
    }
  },
  methods: {
    // 应充值计算逻辑
    shouldRechargeChange(){
      if (this.dialogFormData.totalProfitLoss && this.dialogFormData.profitLossAmend && this.dialogFormData.rate) {
        this.dialogFormData.shouldRecharge = this.dialogFormData.rate * (this.dialogFormData.totalProfitLoss + this.dialogFormData.profitLossAmend)
      }
    },
    // 代充值计算逻辑
    unRechargeChange(){
      if (this.dialogFormData.shouldRecharge && this.dialogFormData.shouldRechargeAmend && this.dialogFormData.thisRecharged) {
        this.dialogFormData.unRecharge = this.dialogFormData.shouldRecharge + this.dialogFormData.shouldRechargeAmend - this.dialogFormData.thisRecharged
      }
    },
    openModal(id) {
      console.log("openModal=",id);
      this.visable = true
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        if (this.isNotBlank(id)) {
          this.title = '修改充值信息'
          // 获取修改的信息
          getItemByID({ 'id': id }).then(res => {
            console.log("修改返回值=",res);
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增充值信息'
        }
        // 获取所有学员信息
        getAll({}).then(res=>{
          if (res.code == 1) {
            this.studentList = res.data
          }
        })
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            addItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  :deep(.el-drawer__title) {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small),
  :deep(.el-select) {
    width: 100%;
  }
  :deep( .el-form-item__content) {
    padding-right: 0px;
  }
</style>
